package wsepForum.ui.jaxcent.states;

import java.util.Collection;
import java.util.Vector;

import jaxcent.Jaxception;

import wsepForum.common.elements.Section;
import wsepForum.ui.jaxcent.IXContext;

public class XSectionCreate extends AXSectionControl {

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public XSectionCreate(IXContext context, Section section) {
		super(context,section);
	}


	/*****************************
	 * METHODS                   *
	 *****************************/
	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#initContent()
	 */
	protected void initContent() {
		super.initContent();
		try {
			// adds  users to moderators list
			Vector<String> users = this.context.getSession().getHandler().getManagedUsernames();
			for (int i=0;i<users.size();i++) {
				String user = users.elementAt(i);
				xModerators.insertOption(i, user);
			}
			this.xTitle.setInnerHTML("Create section");
			this.xSubmit.setValue("Create");
		} catch (Jaxception e) {
			this.context.getPage().showMessageDialog("Cannot put data on page's field, please try reloading the page.\n"+e.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#dealWithSubmit(java.lang.String, java.util.Collection)
	 */
	@Override
	protected void dealWithSubmit(String userName, Collection<String>newModerators) throws Jaxception {
		try {
			if ( userName.equals("")) {
				this.context.getPage().showMessageDialog( "Please enter section name" );
			    return;
			}
			Section modifiedSection = this.context.getSession().getHandler().createSection(section.getElementID(), userName, newModerators);
			this.context.loadSection(modifiedSection);
		} catch (Exception e) {
			this.context.getPage().showMessageDialog( "Unable to create section!\n"+e.getMessage());
			this.context.loadSection(0);
		}
	}

	/* (non-Javadoc)
	 * @see wsepForum.ui.jaxcent.states.AXSectionControl#construct()
	 */
	@Override
	protected void construct() {
		construct(
				"<input type=\"text\" tabindex=\"1\" name=\"xsectionname\" id=\"xsectionname\" size=\"25\" value=\"\" class=\"inputbox autowidth\" />",
				""
		);
	}












}
